package com.learnyou.server.xingzuo.dto.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PageDTO<T> {
    @ApiModelProperty(value = "分页信息")
    private PageInfoDTO pageInfo;

    @ApiModelProperty(value = "查询结果")
    private List<T> result = Collections.emptyList();


    public <M, T>  PageDTO<T> convert(Page<M> page, Function<M, T> modelToDTO) {
        PageDTO<T> pageDTO = new PageDTO<>();
        pageDTO.setPageInfo(PageInfoDTO.build(page.getSize(), page.getCurrent(), page.getTotal()));
        pageDTO.setResult(page.getRecords().stream().map(modelToDTO).collect(Collectors.toList()));
        return pageDTO;
    }
}
